export let CSSMap = [
    'ToggleButton',
    {css:'',control:''}
];


function getControlClass(css_class_name){
    let controlClassName = undefined;
    // console.log('className--',css_class_name)

    CSSMap.forEach(function (one) {
        if (typeof one === 'string'){
            if (one===css_class_name){
                controlClassName = css_class_name;
                return controlClassName
            }
        }
        else if (one instanceof Array){
            if (one.length > 1){
                if (one[0]=== css_class_name){
                    controlClassName = one[1];
                    return controlClassName
                }
            }
        }else {
            if (one instanceof Object){

                if (one.css === css_class_name){
                    controlClassName = one.control;
                    return controlClassName
                }

            }
        }
    });
    return controlClassName;
}
Element.prototype.getOrCreateControl = function (className) {
    let ctr = this.$control;
    if (ctr){
        return ctr
    }
    let ele = this;
    if (className){
        try{
            return eval(`new ${className}(ele)`)
        }catch(e){
        }
    }

    let class_list = this.classList;
    let controlCssClassName = '';
    if (class_list.length > 0){
        controlCssClassName = class_list[0];
    }
    if (controlCssClassName.length > 0){
        let control_class = getControlClass(controlCssClassName);
        if (control_class){
            if (typeof control_class === 'string'){
                ctr = eval(`new ${control_class}(ele)`)
            }
            else if (typeof control_class === 'function'){
                ctr = new control_class(ele)
            }
        }
    }
    if (ctr){
        this.$control = ctr;
    }
    return ctr
};



export class BaseControl {
    static css_class(){
        return 'BaseControl'
    }
    constructor(element){
        element.$control = this;
        this.$el = element;
        this.changeValue = function (v,k) {

        };
        this.init(element);
    }

    init(el){
    }

}
